package com.database.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.database.common.BaseResultCodeEnum;
import com.database.common.ResponseException;
import com.database.common.Tools;
import com.database.dao.AddressMapper;
import com.database.dao.CollectMapper;
import com.database.dao.FoodMapper;
import com.database.dao.MenuMapper;
import com.database.dao.OrderMapper;
import com.database.dao.ShopMapper;
import com.database.dao.StaffMapper;
import com.database.dao.UserMapper;
import com.database.model.Address;
import com.database.model.Collect;
import com.database.model.Food;
import com.database.model.Menu;
import com.database.model.Order;
import com.database.model.Shop;
import com.database.model.Staff;
import com.database.model.User;
import com.database.model.param.OrderParam;
import com.database.model.param.UserParam;
import com.database.model.vo.CollectVo;
import com.database.model.vo.OrderVo;
import com.database.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {

	@Autowired
	private AddressMapper addressMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private ShopMapper shopMapper;
	@Autowired
	private FoodMapper foodMapper;
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private MenuMapper menuMapper;
	@Autowired
	private StaffMapper staffMapper;
	@Autowired
	private CollectMapper collectMapper;

	@Override
	public int insertAddress(Address address) {
		return addressMapper.insert(address);
	}

	@Override
	public User login(User user) {
		if (user.getAccount().equals("")) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_20005);
		} else if (user.getPassword().equals("")) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_20005);
		}
		try {
			boolean value = userMapper.selectByAccount(user).getPassword().equals(Tools.getMD5Str(user.getPassword()));
			if (value) {
				return userMapper.selectByAccount(user);
			} else {
				throw new ResponseException(BaseResultCodeEnum.ERR_CODE_30000);
			}
		} catch (Exception e) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_30000);
		}

	}

	@Override
	public int registeUser(UserParam userParam) {
		if ((!userParam.getPassword().equals(userParam.getCheckPassword()) || userParam.getPassword().equals(""))) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_20005);
		} else if (userParam.getAccount().equals("")) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_20002);
		} else if (userParam.getContact().equals("")) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_20004);
		} else if (userParam.getName().equals("")) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_20018);
		} else {
			try {
				User user = new User(userParam);
				user.setPassword(Tools.getMD5Str(user.getPassword()));
				return userMapper.insert(user);
			} catch (Exception e) {
				throw new ResponseException(BaseResultCodeEnum.ERR_CODE_30001);
			}

		}
	}

	@Override
	public List<Shop> findShopList() {
		return shopMapper.getAll();
	}

	@Override
	public List<Food> findFoodList(Food food) {
		List<Food> foodList = foodMapper.selectByShopId(food);
		if (foodList.size() <= 0) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_30006);
		}
		return foodList;
	}

	@Override
	public int createOrder(OrderParam orderParam) {
		User user = new User();
		user.setUserId(orderParam.getUserId());
		double money = userMapper.selectByKey(user).getBalance();// 查询用户余额
		if (orderParam.getTotalPrice() > money) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_30004);// 用户余额不足
		}
		// 进行菜单数量判断
		List<Menu> foods = orderParam.getFoods();
		for (int i = 0; i < foods.size(); i++) {
			Food food = new Food();
			food.setFoodId(foods.get(i).getFoodId());
			int count = foodMapper.selectByKey(food).getResidue(); // 菜式余量
			if (foods.get(i).getCount() > count) {
				throw new ResponseException(BaseResultCodeEnum.ERR_CODE_30005);//
			}
		}

		Order order = new Order(orderParam);
		orderMapper.insert(order);// 生成订单
		User user2 = userMapper.selectByKey(user);
		user2.setBalance(user2.getBalance() - orderParam.getTotalPrice());
		userMapper.updateByKey(user2);
		int orderId = orderMapper.selectKeyByOrder(order).getOrderId(); // 获取订单ID
		for (int i = 0; i < foods.size(); i++) {
			foods.get(i).setOrderId(orderId);
			menuMapper.insert(foods.get(i));
			Food food = new Food();
			food.setFoodId(foods.get(i).getFoodId());
			Food food2 = foodMapper.selectByKey(food);
			food2.setResidue(food2.getResidue() - foods.get(i).getCount());
			foodMapper.updateByKey(food2); // 更新菜单数量
		}
		return 1;

	}

	@Override
	public List<OrderVo> findOrderList(Order order) {
		List<Order> list = orderMapper.selectByUserId(order);
		List<OrderVo> resultList = new ArrayList<OrderVo>();
		for (int i = 0; i < list.size(); i++) {
			Order order2 = list.get(i);
			OrderVo orderVo = new OrderVo(list.get(i));
			Shop shop = new Shop();
			shop.setShopId(order2.getShopId());
			orderVo.setShopName(shopMapper.selectByKey(shop).getName()); // 设置店主名字
			System.out.println(order2.getStaffId());
			if (order2.getStaffId() != null && order2.getStaffId() > 0) {
				Staff staff = new Staff();
				staff.setStaffId(order2.getStaffId());
				orderVo.setStaffName(staffMapper.selectByKey(staff).getName());
				orderVo.setContact(staffMapper.selectByKey(staff).getContact());
			}
			resultList.add(orderVo);
		}
		Collections.reverse(resultList);
		return resultList;
	}

	@Override
	public List<Menu> findOrder(Menu menu) {
		return menuMapper.selectByKey(menu);
	}

	@Override
	public int estimateOrder(Order order) {
		return orderMapper.updateStatusByKey(order);
	}

	@Override
	public User findBalance(User user) {
		return userMapper.selectByKey(user);
	}

	@Override
	public int addBalance(User user) {
		User user2 = userMapper.selectByKey(user);
		user2.setBalance(user2.getBalance() + user.getBalance());
		return userMapper.updateByKey(user2);
	}

	@Override
	public List<Address> selectAddressByUserId(User user) {
		return addressMapper.selectByUserId(user);
	}

	@Override
	public int deleteAddressByKey(Address address) {
		// TODO Auto-generated method stub
		return addressMapper.deleteByKey(address);
	}

	@Override
	public int updateAddressByKey(Address address) {
		return addressMapper.updateByKey(address);
	}

	@Override
	public int updateUserInfo(User user) {
		// TODO Auto-generated method stub
		return userMapper.updateByKey(user);
	}

	@Override
	public List<CollectVo> findCollectShopList(Collect collect) {
		List<Collect> list = collectMapper.selectByUserId(collect);
		List<CollectVo> resultList = new ArrayList<CollectVo>();
		for (int i = 0; i < list.size(); i++) {
			Collect collect2 = list.get(i);
			CollectVo collectVo = new CollectVo(list.get(i));
			Shop shop = new Shop();
			shop.setShopId(collect2.getShopId());
			collectVo.setShopName(shopMapper.selectByKey(shop).getName()); // 设置店主名字
			resultList.add(collectVo);

		}
		return resultList;
	}

	@Override
	public int aadCollectShop(Collect collect) {
		// TODO Auto-generated method stub
		return collectMapper.insert(collect);
	}

	// 测试
	@Override
	public int testOrder(Order order) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Shop> selectLikeAddress(Shop shop) {
		List<Shop> resultList= shopMapper.selectLikeAddress(shop);
		Food food = new Food();
		food.setName(shop.getAddress());
		List<Integer> list = foodMapper.selectLikeFood(food);
		for(int i=0;i<list.size();i++){
			Shop shop3 = new Shop();
			shop3.setShopId(list.get(i));
			Shop shop2 = shopMapper.selectByKey(shop3);
			if(resultList.contains(shop2)){
				
			}else{
				resultList.add(shop2);
			}
		}
		return resultList;
	}

}
